﻿using System;
using System.Collections.Generic;
using System.Text;

namespace td.Core
{
    public class ToolButton:DevExpress.XtraBars.BarButtonItem,IStatusUpdate
    {
        private object caller;
        private AbstractToolButtonCommand Info;
      

        public ToolButton(AbstractToolButtonCommand info, object caller)
        {
            this.Info = info;
            this.caller = caller;
            this.LargeGlyph = info.largeImage;
            this.ItemShortcut = new DevExpress.XtraBars.BarShortcut();
            this.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(ToolButton_ItemClick);
            this.Enabled = info.IsEnabled;
            if(! (info.ShortCut == null || info.ShortCut.Equals("")) )
                this.ItemShortcut = new DevExpress.XtraBars.BarShortcut(WorkBenchSingleTon.WorkBench.ParseShortcut(info.ShortCut));
            this.UpdateText();
        }

        void ToolButton_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (this.Info != null) this.Info.Run();
        }
        #region 
        /*
        private void CreateDropDownItems()
        {
            base.DropDownItems.Clear();
            for (int i = 0; i < this.Info.SubMenus.Count; i++)
            {
                MenuItemXml xml = this.Info.SubMenus[i];
                if (xml.IsToGroup)
                {
                    ToolStripSeparator separator = new ToolStripSeparator();
                    base.DropDownItems.Add(separator);
                }
                ToolStripMenuItem item = null;
                if (string.Compare(xml.MenuType, "checkbox", true) == 0)
                {
                    item = new MenuCheckBox(xml, this.caller);
                }
                else
                {
                    item = new MenuCommand(xml, this.caller);
                }
                base.DropDownItems.Add(item);
                if (item is IStatusUpdate)
                {
                    ((IStatusUpdate) item).UpdateStatus();
                    ((IStatusUpdate) item).UpdateText();
                }
            }
        }

        protected override void OnDropDownOpened(EventArgs e)
        {
            ArrayList list = new ArrayList();
            foreach (ToolStripItem item in base.DropDownItems)
            {
                if (item != null)
                {
                    ToolStripItem item2 = item;
                    if (item2 is ToolStripSeparator)
                    {
                        item2.Visible = true;
                    }
                    if (item2.Visible)
                    {
                        list.Add(item);
                    }
                }
            }
            for (int i = 0; i < list.Count; i++)
            {
                object obj2 = list[i];
                if (obj2 is ToolStripSeparator)
                {
                    if (i == 0)
                    {
                        ((ToolStripSeparator) obj2).Visible = false;
                        list.Remove(obj2);
                        i--;
                    }
                    else
                    {
                        object obj3 = list[i - 1];
                        if (obj3 is ToolStripSeparator)
                        {
                            ((ToolStripSeparator) obj3).Visible = false;
                        }
                        if (i == (list.Count - 1))
                        {
                            ((ToolStripSeparator) obj2).Visible = false;
                        }
                    }
                }
            }
            base.OnDropDownOpened(e);
        }

        protected override void OnDropDownShow(EventArgs e)
        {
            if ((this.Info != null) && !base.DropDown.Visible)
            {
                foreach (ToolStripItem item in base.DropDownItems)
                {
                    if (item is IStatusUpdate)
                    {
                        ((IStatusUpdate) item).UpdateStatus();
                        ((IStatusUpdate) item).UpdateText();
                    }
                }
            }
            base.OnDropDownShow(e);
        }
        */
        #endregion
        public virtual void UpdateStatus()
        {
            if (this.Info != null)
            {
                base.Enabled = this.Info.IsEnabled;
            }
        }

        public virtual void UpdateText()
        {
            if (this.Info != null)
            {
                this.Caption = this.Info.Caption;
            }
        }

        //public override bool Enabled
        //{
        //    get
        //    {
        //        if (this.Info == null)
        //        {
        //            return base.Enabled;
        //        }
        //        return true;
        //    }
        //}
    }
}
